//
//  ImageBrowserManager.m
//  SSSMerchant
//
//  Created by 谢曦 on 2017/4/2.
//  Copyright © 2017年 xnxin. All rights reserved.
//

#import "ImageBrowserManager.h"
#import "MWPhotoBrowser.h"
@interface ImageBrowserManager ()<MWPhotoBrowserDelegate>
@property (nonatomic,strong) NSArray *MWPhotos;
//@property (nonatomic,strong) MWPhotoBrowser *browser;
@end
@implementation ImageBrowserManager
+ (instancetype)browserManager
{
    static ImageBrowserManager *browserManager = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        browserManager = [[ImageBrowserManager alloc] init];
//        browserManager.browser = [[MWPhotoBrowser alloc] initWithDelegate:browserManager];//不可以被多个控制器重用

    });
    return browserManager;
}

+(void)viewController:(UIViewController *)vc  pushToBrowserWithPhotos:(NSArray <UIImage *>*)photos {
    [ImageBrowserManager viewController:vc pushToBrowserWithPhotos:photos currentIndex:0];

}
+(void)viewController:(UIViewController *)vc  pushToBrowserWithPhotos:(NSArray <UIImage *>*)photos currentIndex:(NSInteger )index{
    [ImageBrowserManager browserManager].MWPhotos=@[];//清零
    NSMutableArray *arr=@[].mutableCopy;
    for (UIImage *image in photos) {
        MWPhoto *photo = [MWPhoto photoWithImage:image];
        [arr addObject:photo];
    }
    MWPhotoBrowser *browser=    [[MWPhotoBrowser alloc] initWithDelegate:[ImageBrowserManager browserManager]];
    [ImageBrowserManager browserManager].MWPhotos=arr.copy;
    [browser setCurrentPhotoIndex:index];
    [vc.navigationController pushViewController:browser animated:YES];
    
    
}

#pragma mark - MWPhotoBrowserDelegate
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return   [ImageBrowserManager browserManager].MWPhotos.count;
}
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < _MWPhotos.count) {
        return [[ImageBrowserManager browserManager].MWPhotos objectAtIndex:index];
    }
    return nil;
}
@end
